package com.alk.frijava.candlestickchart;

import javafx.scene.Group;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.GridPane;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;


    class Candle extends Group {
    	private int nLine = 8;
    	private Line [] candleLine = new Line[nLine];
    	private Rectangle candlRectangle = new Rectangle();
    	private String seriesStyleClass;
        private String dataStyleClass;
        private boolean openAboveClose = true;
        private Tooltip tooltip = new Tooltip();

        public Candle(String seriesStyleClass, String dataStyleClass) {
        	
        	setAutoSizeChildren(false);
        	for( int i=0; i<nLine; i++ ) {
        		candleLine[i] = new Line();
        		getChildren().add(candleLine[i]);
        	}
        	getChildren().add(candlRectangle);
            this.seriesStyleClass = seriesStyleClass;
            this.dataStyleClass = dataStyleClass;
        }

        public void setSeriesAndDataStyleClasses(String seriesStyleClass, String dataStyleClass) {
            this.seriesStyleClass = seriesStyleClass;
            this.dataStyleClass = dataStyleClass;
        }

        public void update(double yOffSet, double open, double close, double high, double low, double candleWidth) {

        	double openOffset = open-yOffSet;
        	double closeOffset = close-yOffSet;
        	double highOffset = high-yOffSet;
        	double lowOffset = low-yOffSet;
            
            //y-axis is reversed
            openAboveClose = open < close;
            
            if( !openAboveClose ){
            	candlRectangle.setX(-candleWidth/2);
            	candlRectangle.setY(closeOffset);
            	candlRectangle.setHeight(open-close);
            	candlRectangle.setWidth(candleWidth);
            }
            
            candleLine[0].setStartY(highOffset);
            if( openAboveClose ) {
            	candleLine[0].setEndY(openOffset);
            } else {
            	candleLine[0].setEndY(closeOffset);
            }
            
            candleLine[7].setStartY(lowOffset);
            if( openAboveClose ) {
            	candleLine[7].setEndY(closeOffset);
            } else {
            	candleLine[7].setEndY(openOffset);
            }
            
            
            candleLine[1].setStartX(-candleWidth/2);
            candleLine[1].setStartY(highOffset);
            candleLine[1].setEndX(candleWidth/2);
            candleLine[1].setEndY(highOffset);
            
            candleLine[2].setStartX(-candleWidth/2);
            candleLine[2].setStartY(lowOffset);
            candleLine[2].setEndX(candleWidth/2);
            candleLine[2].setEndY(lowOffset);
            
            candleLine[3].setStartY(openOffset);
            candleLine[3].setStartX(candleWidth/2);
            candleLine[3].setEndY(closeOffset);
            candleLine[3].setEndX(candleWidth/2);
            
            candleLine[4].setStartY(closeOffset);
            candleLine[4].setStartX(candleWidth/2);
            candleLine[4].setEndY(closeOffset);
            candleLine[4].setEndX(-candleWidth/2);
            
            candleLine[5].setStartY(closeOffset);
            candleLine[5].setStartX(-candleWidth/2);
            candleLine[5].setEndY(openOffset);
            candleLine[5].setEndX(-candleWidth/2);
            
            candleLine[6].setStartY(openOffset);
            candleLine[6].setStartX(-candleWidth/2);
            candleLine[6].setEndY(openOffset);
            candleLine[6].setEndX(candleWidth/2);           
        }
    }